<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'detail';
$return=array();
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){
	
	exi($return);
}
/*******op*******/

//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
	exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//拼团活动信息
	$group=pdo_get('jy_aligroup_groups',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['groupid']));	
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['goodsid']));	
	$order['createtime']=date('Y-m-d h:i',$order['createtime']);
	$order['paytime']=date('Y-m-d h:i',$order['paytime']);
	$order['sendtime']=date('Y-m-d h:i',$order['sendtime']);
	$order['finishtime']=date('Y-m-d h:i',$order['finishtime']);
	$order['refundtime']=$order['refundtime']?date('Y-m-d h:i',$order['refundtime']):'';
	$order['statustitle']=$this->order_status[$order['status']];
	//核销
	$verify=array();
	$verify['verifycode']=$order['verifycode'];
	if($verify['verifycode']){
		$verifynum=$this->pdo_count('groups_verify',array('orderid'=>$order['id']));
		$verify['lastverifys']=$order['verifynum']-$verifynum;
		$verify['url']=custom_url('jy_aligroup/verify.detail//id='.$order['id'].'&verifycode='.$verify['verifycode'],true);
		//小程序二维码：
		$url='/jy_aligroup/verify/detail/index?id='.$order['id'].'&verifycode='.$verify['verifycode'];
		$pid='verifyid'.$order['id'].'c'.$verify['verifycode'];
		$verify['appcode'] = model('jy_weishop/model')->get_wxappewm($url,$pid);
	}
	$order['verify']=$verify;
	$return['order']=$order;
	if(!$group['feetype'] && $group['agio']!=0){
			$group['group_price']=price($goods['price']*$group['agio']/100);
	}elseif($group['feetype'] && $group['price']!=0){
			$group['group_price']=$group['price'];
	}else{
			$group['group_price']=price($goods['price']);
	}
	$return['group']=$group;
	$return['member']=$member;
	$return['goods']=$goods;
	$stores = array();
	$goods['storeids'] = json_decode($goods['storeids'],true);
	if ($goods['isverify'] == 1) {
		if (empty($goods['storeids'])) {
			$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
		}else {
			foreach($goods['storeids'] as $sv){
			$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
			}
		}
	}elseif($shopset['openstore'] ==1){
		if (empty($goods['storeids'])) {
			$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
		}else {
			foreach($goods['storeids'] as $sv){
				$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
			}
		}
	}
	$return['stores']=$stores;
	$return['store']=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['storeid']));
	$return['address'] =  pdo_get('core_address',array('id'=>$order['addressid'],'uniacid'=>$_SESSION['uniacid']));
	exi($return);
}

if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除成功');
}
if($_RQ['op']=='pay'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$params['fee']=$order['price'];
	$params['title']='阿里拼团订单';
	$params['id']=$_RQ['id'];
	$params['plugin']='jy_aligroup';
	$params['back_url']='jy_aligroup/orderdetail/payresult';
	cfc('core')->pay($params);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['payresult'];unset($_SESSION['payresult']);
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_aligroup_groups_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_aligroup_groups_order',array('status'=>1,'paytime'=>TIMESTRAMP),array('id'=>$order['id']));
	exi('支付成功','success','jy_aligroup/order//status=1');
}